from rest_framework import serializers
from api.models.user import User, Address, ShipAddress
import re

from api.utils.serializers import PaginationResultSerializer


class AddressQuerySerializer(serializers.Serializer):
    username = serializers.CharField(help_text="用户名", max_length=256, default="")
    default = serializers.BooleanField(help_text="默认地址", default=False)
    page = serializers.IntegerField(help_text="页数", default=1)
    per_page = serializers.IntegerField(help_text="数量", default=10)


class UserBodySerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = "__all__"


class AddressBodySerializer(serializers.ModelSerializer):
    user = UserBodySerializer()

    class Meta:
        model = ShipAddress
        fields = "__all__"


class AddressDataSerializer(PaginationResultSerializer):
    data = AddressBodySerializer(many=True)


class UserQuerySerializer(serializers.Serializer):
    username = serializers.CharField(help_text="用户名", max_length=256, required=True)


class UserAddressQeurySerializer(serializers.ModelSerializer):
    address_id = serializers.IntegerField(help_text="地址ID", default=0)

    class Meta:
        model = ShipAddress
        fields = [
            "address_id",
            "province",
            "city",
            "area",
            "detailed",
            "default",
        ]


class UserAddressBodySerializer(serializers.ModelSerializer):
    class Meta:
        model = ShipAddress
        fields = "__all__"


class UserInfoSerializer(serializers.ModelSerializer):
    address = UserAddressBodySerializer(many=True)

    class Meta:
        model = User
        fields = "__all__"


class AddressQeurySerializer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = ["parent_number"]

        extra_kwargs = {
            "parent_number": {"required": False},
        }


class AddressBobySerializer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = [
            "number",
            "parent_number",
            "pca",
        ]


class UserLoginRequestSerializer(serializers.ModelSerializer):

    password2 = serializers.CharField(help_text="密码2", required=True)
    username = serializers.CharField(help_text="用户名", required=True)
    allow = serializers.BooleanField(help_text="是否同意用户协议", required=False, default=False)

    def validate(self, attrs):
        if attrs["password"] != attrs["password2"]:
            raise serializers.ValidationError("密码不一致")
        if not re.match("^[a-zA-Z0-9]{8,20}$", attrs["username"]):
            raise serializers.ValidationError("用户名不正确")

        return attrs

    class Meta:
        model = User
        fields = ["mobile", "username", "password", "password2", "allow"]


class UserNameRepeatCheckSerializer(serializers.Serializer):
    username = serializers.CharField(help_text="用户名", required=True)

    def validate_username(self, username):
        if not re.match("^[a-zA-Z0-9]{8,20}$", username):
            raise serializers.ValidationError("用户名不正确")
        return username


class MobileRepeatCheckSerializer(serializers.Serializer):
    mobile = serializers.CharField(help_text="手机号", required=True)

    def validate_mobile(self, mobile):
        if not re.match("^1[1-3]\d{9}$", mobile):
            raise serializers.ValidationError("手机号不正确")
        return mobile


class RepeatRepeatSerializer(serializers.Serializer):
    mob = serializers.CharField(help_text="手机号")


class UserBodySerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "username"]
